let App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopList: [],
    merchants: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.zk)

    if (options.zk) {
      // 如果是电子券进来的话
      this.setData({
        canshu: options.pag
      })
    } else {
  

    }





    this.onPullDownRefresh();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading()
    this.initData()
    this.getList(() => {
      wx.hideNavigationBarLoading()
      wx.stopPullDownRefresh()
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let merchants = this.data.merchants;
    if (merchants.items.length === merchants.total) {
      return;
    }
    this.getList();
  },

  /**
   * 用户点击右上角分享
   * 
   */
  onShareAppMessage: function () {

  },
  initData() {
    const merchants = {
      items: [],
      total: '',
      params: {
        page: 1,
        pageCount: 5
      }
    }
    this.setData({
      merchants: merchants
    })
  },
  // 跳转到洗车详情页
  toDetail(e) {
    let dataset = e.currentTarget.dataset;
    let shopId = dataset.id;
    let img = dataset.img;
    let phone = dataset.phone;
    let sale = dataset.sale;
    let shopname = dataset.shopname;
    let time = dataset.time;
    let address = dataset.address;
    let distance = dataset.distance;
     let canshu =this.data.canshu

    wx.navigateTo({
      url: `/pages/wash-car/detail/detail?shopId=${shopId}&img=${img}&phone=${phone}&sale=${sale}&shopname=${shopname}&time=${time}&address=${address}&distance=${distance}&canshu=${canshu}`,
    })
  },
  // 获取洗车列表
  getList(cb) {
    let { merchants } = this.data;
    let sellerId = wx.getStorageSync("agent").id;
    let userId = wx.getStorageSync("userId");
    let userLocation = wx.getStorageSync("position").location;
    let arr = userLocation.split(",");
    let lng = arr[0];
    let lat = arr[1];
    let saleCount = 0;
    let distance = 0;
    let params = merchants.params;
    params.sellerId = sellerId;
    params.userId = userId;
    params.lng = lng;
    params.lat = lat;
    params.saleCount = saleCount;
    params.distance = distance;
    App.HttpService.washCarList(params).then(res => {
      cb && cb();
      if (res.data.code == 200) {
        let data = res.data.data;
        merchants.items = [...merchants.items, ...data.shopList];
        if (data.shopList.length > 0) {
          params.page = params.page + 1;
          merchants.total = data.counts;
        }
        this.setData({ merchants });
      }
    });
  },
  // 按照距离最近排序
  distance() {
    this.initData();
    let { merchants } = this.data;
    let sellerId = wx.getStorageSync("agent").id;
    let userId = wx.getStorageSync("userId");
    let userLocation = wx.getStorageSync("position").location;
    let arr = userLocation.split(",");
    let lng = arr[0];
    let lat = arr[1];
    let page = 1;
    let pageCount = 5;
    let saleCount = 0;
    let distance = 1;
    let params = merchants.params;
    params.sellerId = sellerId;
    params.userId = userId;
    params.lng = lng;
    params.lat = lat;
    params.saleCount = saleCount;
    params.distance = distance;
    App.HttpService.washCarList(params).then(res => {
      if (res.data.code == 200) {
        let data = res.data.data;
        merchants.items = [...merchants.items, ...data.shopList];
        if (data.shopList.length > 0) {
          params.page = params.page + 1;
          merchants.total = data.counts;
        }
        this.setData({ merchants });
      }
    });
  },
  // 按照销量最高排序
  sale() {
    this.initData();
    let { merchants } = this.data;
    let sellerId = wx.getStorageSync("agent").id;
    let userId = wx.getStorageSync("userId");
    let userLocation = wx.getStorageSync("position").location;
    let arr = userLocation.split(",");
    let lng = arr[0];
    let lat = arr[1];
    let page = 1;
    let pageCount = 5;
    let saleCount = 1;
    let distance = 0;
    let params = merchants.params;
    params.sellerId = sellerId;
    params.userId = userId;
    params.lng = lng;
    params.lat = lat;
    params.saleCount = saleCount;
    params.distance = distance;
    App.HttpService.washCarList(params).then(res => {
      if (res.data.code == 200) {
        let data = res.data.data;
        merchants.items = [...merchants.items, ...data.shopList];
        if (data.shopList.length > 0) {
          params.page = params.page + 1;
          merchants.total = data.counts;
        }
        this.setData({ merchants });
      }
    });
  }
})